<?php
session_start();
include("mysqlcon.php");

include("ok.php");
include("error.php");
	//$roomType = $_POST[roomType];
	//$roomID = $_POST[roomID];
	$ref = $_GET['ref'];
	$extra_bed = $_POST['extraBed'];
	$customer_name = $_POST['customer_name'];
	$customer_surname = $_POST['customer_surname'];
	$customer_id = $_POST['customer_id'];
	$customer_address = $_POST['customer_address'];
	$customer_email = $_POST['customer_email'];
	$customer_tel = $_POST['customer_tel'];
	$customer_fax = $_POST['customer_fax'];
	//$reserv_etc = $_POST['reserv_etc'];
	$reserv_gdetail = $_POST['reserv_gdetail'];
 	$inputDate = $_POST['inputDate'];
 	$chkInputEditDate = $_POST['chkInputEditDate'];
 	$reservID = $_POST['reserv_id'];
 	$reserv_status = $_POST['reserv_status'];
 	$show_extra_bed = $extra_bed == 0 ? '-':$extra_bed;
 	$customer_type = $_POST['customer_type'];
	$customer_group_name = $customer_type == 1 ? $_POST['customer_group_name'] : '';
	$userID = $_SESSION["HBD_USER"];
	
	$arrayList = array();
	
	if($customer_name ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : ชื่อ<br>";}
   if($customer_surname ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : นามสกุล<br>";}
   //if($customer_id ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : รหัสบัตรประจำตัวประชาชน<br>";}
   //if($customer_email ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : อีเมล์<br>";}
   //if($customer_tel ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : เบอร์โทรศัพท์<br>";}
   if($inputDate ==""){$error=true; $error_text.= "คุณไม่ได้เลือก : ระยะเวลาในการพัก<br>";}

	if($error==true) error_page($error_text);
	
	if($error==false){
      list($bYear,$bMonth,$bDay) = explode("-",substr($inputDate,0,10));
      list($eYear,$eMonth,$eDay) = explode("-",substr($inputDate,11,10));

      $bDate = mktime(0,0,0,$bMonth,$bDay,$bYear) . "<br>";
      $eDate = mktime(0,0,0,$eMonth,$eDay,$eYear);

      $reserv_begin = date("Y-m-d", $bDate);
      $reserv_end = date("Y-m-d", $eDate);

	
      mysql_select_db($dbname,$conn);
      mysql_query('SET NAMES utf8');

	  $chkID = "SELECT MAX(id) FROM customer";
	  $nextID = 0;
	  $roomListArray = Array();
	  $chkIDresult = mysql_query($chkID);
	  while($rs = mysql_fetch_array($chkIDresult))
	  {
		 $nextID =  $rs[0]+1;
	  }
	  
		$sql = "SELECT * FROM roomtype";
		$r = mysql_query($sql,$conn);
		while ($rs = mysql_fetch_array($r))
		{		   
		   $sql2 = "SELECT room_id FROM resort,roomtype WHERE resort.roomtype_id = $rs[roomtype_id] AND room_id NOT IN(SELECT DISTINCT reservation.room_id FROM reservation WHERE reserv_status > '2' AND (`reserv_end` > '$reserv_begin' AND `reserv_end` <= '$reserv_end'
		   OR `reserv_begin` >= '$reserv_begin' AND `reserv_begin` <= '$reserv_end') AND reservation.reserv_gid != ".$reservID.") AND resort.roomtype_id = roomtype.roomtype_id ORDER BY resort.room_id ASC";
		   $r2 = mysql_query($sql2,$conn); 
		   while ($rs2 = mysql_fetch_array($r2))
			{
				 //$myArray[$i++] = $_POST["roomlist_".$rs2[room_id]];
				 $_POST["roomlist_".$rs2[room_id]] == $rs2[room_id] ? array_push($roomListArray,$_POST["roomlist_".$rs2[room_id]]) : '';
				//array_push($myArray,$_POST["roomlist_".$rs2[room_id]]);
			}
		}
		$roomID = implode ( ", ", $roomListArray );
		if($roomID ==""){$error=true; $error_text.= "คุณไม่ได้เลือก : ห้องพัก<br>";}
		if($error==true){error_page($error_text); exit();}
      $chkCustomer="SELECT * FROM customer WHERE customer_name = '".$customer_name."' AND customer_surname = '".$customer_surname."'";
	  $chkResult = mysql_query($chkCustomer,$conn);
	  
	if($customer_id==null)
	{
		if(mysql_num_rows($chkResult)==0)
		{
			 $customerSQL="INSERT INTO customer(customer_name,customer_surname,customer_email,customer_tel,customer_fax,customer_group_name)";
				$customerSQL=$customerSQL."VALUES('$customer_name','$customer_surname','$customer_email','$customer_tel','$customer_fax','$customer_group_name')";
						if(!mysql_query($customerSQL,$conn)){
						echo("ฐานข้อมูลผิดพลาด $customerSQL");}
		}
		else
		{
			while($rs = mysql_fetch_array($chkResult))
			{
				$nextID = $rs[0];	
			}
		}
			$reservgroupSQLnonMem="UPDATE reservgroup SET 
			  						customer_id = '$nextID'
			  						,room_list = '$roomID'
			  						,reserv_gdetail = '$reserv_gdetail'
			  						,reserv_owner_id = '$userID'
			  					WHERE reserv_gid = '$reservID'";
			if(!mysql_query($reservgroupSQLnonMem,$conn)){echo("ฐานข้อมูลผิดพลาด $reservgroupSQLnonMem"); exit();}
				
			$reservSQLSel = "SELECT * FROM reservation WHERE reserv_gid = '$reservID'";
			$resultReservSel = mysql_query($reservSQLSel);
			while($r = mysql_fetch_array($resultReservSel))
				{	
					$room_id = $r['room_id'];
					$arrayList[$room_id] = $r['reserv_disc'];
				}

			$reservSQLDelnonMem = "DELETE FROM reservation WHERE reserv_gid = '$reservID'";
			if(!mysql_query($reservSQLDelnonMem,$conn)){echo("ฐานข้อมูลผิดพลาด $reservSQLDelnonMem"); exit();}
			
			$index_count = 0;
			$text_room_list = '
				<thead>
				<tr>
				<th scope="col" >ห้องพัก</th>
				<th scope="col" >ราคา</th>
				<th scope="col" >จำนวน(คืน)</th>
				<th scope="col" >เตียงเสริม</th>
				<th scope="col" >รายละเอียดเพิ่มเติม</th>
				<th scope="col" >จำนวนเงิน</th>
				<th scope="col" >ส่วนลด(%)</th>
				<th scope="col" >คิดเป็น</th>
				</tr>	
				</thead>';
			$overall_price = 0;
			$overall_price_disc = 0;
			$all_extrabed  = 0;
			
			foreach($roomListArray as $t){ 
				$reserv_etc_1 = $_POST['reserv_etc_'.$t.''];
				$reserv_extra_bed_1 = $_POST['extraBed_'.$t.''];
				$all_extrabed += $reserv_extra_bed_1;
				$disc = $arrayList[$t] == 0 ? '' : $arrayList[$t];
				$reservSQLnonMem="INSERT INTO reservation(reserv_begin,reserv_end,room_id,id,reserv_etc,reserv_gid,extra_bed,reserv_status)";
				$reservSQLnonMem=$reservSQLnonMem."VALUES('$reserv_begin','$reserv_end','$t','$nextID','$reserv_etc_1','$reservID','$reserv_extra_bed_1','$reserv_status')";
				if(!mysql_query($reservSQLnonMem,$conn)){ echo("ฐานข้อมูลผิดพลาด $reservSQLnonMem"); exit();}
				
				$raw_roomcost = mysql_query("SELECT room_cost FROM resort WHERE room_id = '$t'");
				$raw_roomcost = mysql_fetch_array($raw_roomcost);
				$room_cost = $raw_roomcost['room_cost'];
					
				$dateDiff = strtotime($reserv_begin) - strtotime($reserv_end);
				$fullDays = abs(floor($dateDiff/(60*60*24)));
	
				$full_checkin_time = date("d/m/Y",$reserv_begin);
				$full_now = date("d/m/Y",$reserv_end);
				//price calc
				
				//room charge
				$overall_roomprice = $room_cost*$fullDays;
				$discount = ($overall_roomprice*$disc)/100;
				$roomPriceDisc = $overall_roomprice - $discount;
				$overall_price_disc += $roomPriceDisc;
				$overall_price += $overall_roomprice;

				
				if($index_count%2==0){$text_room_list .= '<tr class="odd">';}
				else{$text_room_list .= '<tr>';}

				$text_room_list .= "<td>".$t."</td>";
				$text_room_list .= "<td>".$room_cost."</td>";
				$text_room_list .= "<td>".$fullDays."</td>";
				$text_room_list .= "<td>".$reserv_extra_bed_1."</td>";
				$text_room_list .= "<td>".$reserv_etc_1."</td>";
				$text_room_list .= "<td><div id=".$t.">".$overall_roomprice."</div></td>";
				$text_room_list .= "<td><input id='discount_".$t."' type='text' size='3' value='$disc' autocomplete='off' onKeyup=calByRoom('$t');></td>";
				$text_room_list .= "<td><div id=overall_roomprice_".$t." class='overall_roomprice'>".$roomPriceDisc."</div> + ".($reserv_extra_bed_1*400)."</td>";
				$text_room_list .= "</tr>";
			}
			
			$overall_price = $overall_price+($all_extrabed*400);
			$overall_price_disc = $overall_price_disc+($all_extrabed*400);
			$text_room_list .= "<tr style='background-color:#D5B991;'><td colspan='7' style='text-align:right;padding-right:30px;'><h4>ทั้งหมด</h4></td><td style='color:#CE1815;'><h3 id='overall_price'>$overall_price_disc</h3><input id='overall_price' type='hidden' value='".$overall_price."'><input id='overall_price_discounted' type='hidden' value='$overall_price_disc'></td></tr>";
			$ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p style='float:left;'><h2 style='float:right;'></h2></p><input type='hidden' id='reserv_gid' name='reserv_gid' value='$reservID' /><input type='hidden' id='all_extrabed' name='all_extrabed' value='$all_extrabed' /><input type='hidden' id='roomList' name='roomList' value='$roomID' />";
			
	  
			/*
$ok_text = "เลขที่ใบจอง : $reservID<br>
			ห้องหมายเลข : $roomID<br>
			ชื่อ-นามสกุล ผู้จอง : $customer_name $customer_surname<br>
			ที่อยู่ : $customer_address<br>
			E-Mail : $customer_email<br>
			เบอร์โทรศัพท์ : $customer_tel<br>
			Fax : $customer_fax<br>
			ระยะเวลาในการพัก : $inputDate<br>
			รายละเอียดอื่นๆ : $reserv_gdetail<br>";
*/
			ok_page("แก้ไขข้อมูลเรียบร้อย","ระบบทำการแก้ไขข้อมูลการจองแล้ว",$ok_text,"admin.php",$ok_text);
			mysql_close($conn);

	}
	else
	{
			if(mysql_num_rows($chkResult)==0)
			{
				 $customerSQL="INSERT INTO customer(customer_name,customer_surname,customer_address,customer_id,customer_email,customer_tel,customer_fax,customer_group_name)";
					$customerSQL=$customerSQL."VALUES('$customer_name','$customer_surname','$customer_address','$customer_id','$customer_email','$customer_tel','$customer_fax','$customer_group_name')";
							if(!mysql_query($customerSQL,$conn)){
							echo("ฐานข้อมูลผิดพลา $customerSQL");}
			}
			else
			{
				while($rs = mysql_fetch_array($chkResult))
				{
					$nextID = $rs[0];	
				}
			}
			
			$reservgroupSQL="UPDATE reservgroup SET 
			  						customer_id = '$nextID'
			  						,room_list = '$roomID'
			  						,reserv_gdetail = '$reserv_gdetail'
			  						,reserv_owner_id = '$userID'
			  				WHERE reserv_gid = '$reservID'";
			if(!mysql_query($reservgroupSQL,$conn)){echo("ฐานข้อมูลผิดพลาด $reservgroupSQL"); exit();}
				
			$reservSQLSel = "SELECT * FROM reservation WHERE reserv_gid = '$reservID'";
			$resultReservSel = mysql_query($reservSQLSel);
			while($r = mysql_fetch_array($resultReservSel))
				{	
					$room_id = $r['room_id'];
					$arrayList[$room_id] = $r['reserv_disc'];
				}

			$reservSQLDel = "DELETE FROM reservation WHERE reserv_gid = '$reservID'";
			if(!mysql_query($reservSQLDel,$conn)){echo("ฐานข้อมูลผิดพลาด $reservSQLDel"); exit();}
			
			/*
$text_room_list = '
		<thead>
		<tr>
		<th scope="col" >ห้องพัก</th>
		<th scope="col" >ราคา</th>
		<th scope="col" >จำนวน(วัน)</th>
		<th scope="col" >เตียงเสริม</th>
		<th scope="col" >รายละเอียดเพิ่มเติม</th>
		<th scope="col" >จำนวนเงิน</th>
		</tr>	
		</thead>';
*/
		$index_count = 0;
		$text_room_list = '
			<thead>
			<tr>
			<th scope="col" >ห้องพัก</th>
			<th scope="col" >ราคา</th>
			<th scope="col" >จำนวน(คืน)</th>
			<th scope="col" >เตียงเสริม</th>
			<th scope="col" >รายละเอียดเพิ่มเติม</th>
			<th scope="col" >จำนวนเงิน</th>
			<th scope="col" >ส่วนลด(%)</th>
			<th scope="col" >คิดเป็น</th>
			</tr>	
			</thead>';
		$overall_price = 0;
		$all_extrabed  = 0;	
			foreach($roomListArray as $t){ 
				$reserv_etc_2 = $_POST['reserv_etc_'.$t.''];
				$reserv_extra_bed_2 = $_POST['extraBed_'.$t.''];
				$all_extrabed += $reserv_extra_bed_2;
				$disc = $arrayList[$t] == 0 ? '' : $arrayList[$t];
				$reservSQL="INSERT INTO reservation(reserv_begin,reserv_end,room_id,customer_id,id,reserv_etc,reserv_gid,extra_bed,reserv_status)";
				$reservSQL=$reservSQL."VALUES('$reserv_begin','$reserv_end','$t','$customer_id','$nextID','$reserv_etc_2','$reservID','$reserv_extra_bed_2','$reserv_status')";
				if(!mysql_query($reservSQL,$conn)){ echo("ฐานข้อมูลผิดพลาด $reservSQL"); exit();}
				
				$raw_roomcost = mysql_query("SELECT room_cost FROM resort WHERE room_id = '$t'");
				$raw_roomcost = mysql_fetch_array($raw_roomcost);
				$room_cost = $raw_roomcost['room_cost'];
					
				$dateDiff = strtotime($reserv_begin) - strtotime($reserv_end);
				$fullDays = abs(floor($dateDiff/(60*60*24)));
	
				$full_checkin_time = date("d/m/Y",$reserv_begin);
				$full_now = date("d/m/Y",$reserv_end);
				//price calc
				
				//room charge
				$overall_roomprice = $room_cost*$fullDays;
				$discount = ($overall_roomprice*$disc)/100;
				$roomPriceDisc = $overall_roomprice - $discount;
				$overall_price_disc += $roomPriceDisc;
				$overall_price += $overall_roomprice;

				
				if($index_count%2==0){$text_room_list .= '<tr class="odd">';}
				else{$text_room_list .= '<tr>';}

				$text_room_list .= "<td>".$t."</td>";
				$text_room_list .= "<td>".$room_cost."</td>";
				$text_room_list .= "<td>".$fullDays."</td>";
				$text_room_list .= "<td>".$reserv_extra_bed_2."</td>";
				$text_room_list .= "<td>".$reserv_etc_2."</td>";
				$text_room_list .= "<td><div id=".$t.">".$overall_roomprice."</div></td>";
				$text_room_list .= "<td><input id='discount_".$t."' type='text' size='3' value='$disc' autocomplete='off' onKeyup=calByRoom('$t');></td>";
				$text_room_list .= "<td><div id=overall_roomprice_".$t." class='overall_roomprice'>".$roomPriceDisc."</div> + ".($reserv_extra_bed_2*400)."</td>";
				$text_room_list .= "</tr>";

			}
			$overall_price = $overall_price+($all_extrabed*400);
			$text_room_list .= "<tr style='background-color:#D5B991;'><td colspan='7' style='text-align:right;padding-right:30px;'><h4>ทั้งหมด</h4></td><td style='color:#CE1815;'><h3 id='overall_price'>$overall_price_disc</h3><input id='overall_price' type='hidden' value='".$overall_price."'><input id='overall_price_discounted' type='hidden' value='$overall_price_disc'></td></tr>";
			$ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p style='float:left;'><h2 style='float:right;'></h2></p><input type='hidden' id='reserv_gid' name='reserv_gid' value='$reservID' /><input type='hidden' id='all_extrabed' name='all_extrabed' value='$all_extrabed' /><input type='hidden' id='roomList' name='roomList' value='$roomID' />";
			
			/* $text_room_list .= "<tr style='background-color:#D5B991;'><td colspan='5' style='text-align:right;padding-right:30px;'><h4>ทั้งหมด</h4></td><td style='color:#CE1815;'><h3 id='overall_price'>".$overall_price."</h3><input id='overall_price' type='hidden' value='".$overall_price."'><input id='overall_price_discounted' type='hidden' value='$overall_price'></td></tr>"; */
			
			/*
$ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p style='float:left;'><h2 style='float:right;'><label for='discount'>ส่วนลด : </label> <input type='text' id='discount' name='discount' value='' size='8' /> %</h2></p>
				<input type='hidden' id='reserv_gid' name='reserv_gid' value='$reservID' />";
*/
	  
/*
			$ok_text = "เลขที่ใบจอง : $reservID<br>
			ห้องหมายเลข : $roomID<br>
			ชื่อ-นามสกุล ผู้จอง : $customer_name $customer_surname<br>
			ที่อยู่ : $customer_address<br>
			E-Mail : $customer_email<br>
			เบอร์โทรศัพท์ : $customer_tel<br>
			Fax : $customer_fax<br>
			ระยะเวลาในการพัก : $inputDate<br>
			รายละเอียดอื่นๆ : $reserv_gdetail<br>";
*/
			ok_page("แก้ไขข้อมูลเรียบร้อย","ระบบทำการแก้ไขข้อมูลการจองแล้ว",$ok_text,"admin.php",$ok_text);
			mysql_close($conn);
				   
	}
}
?>